package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;

import java.util.ArrayList;
import java.util.List;

public class MainActivity2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        BarChart barChart=findViewById(R.id.bar_chart2);
        List<IBarDataSet> s=new ArrayList<>();

        float[]f1={22f,18f,24f,26f,30f};
        BarDataSet bar1=getBarDataSet(f1,"bar1");
        bar1.setColor(Color.parseColor("#78cdd1"));
        s.add(bar1);

        float[]f2={15f,30f,20f,20f,34f};
        BarDataSet bar2=getBarDataSet(f1,"bar2");
        bar1.setColor(Color.parseColor("#FF0000"));
        s.add(bar2);

        BarData barData=new BarData(s);
        barChart.setData(barData);
        barChart.invalidate();
    }
    private BarDataSet getBarDataSet(float[]f,String title){
        List<BarEntry>data=new ArrayList<>();
        for (int x=0;x<f.length;x++){
            BarEntry barEntry = new BarEntry(x, f[x]);
            data.add(barEntry);
        }
        return new BarDataSet(data, title);
    }
}
